//
//  XMLoadingView.swift
//  zhonglb_iOS
//
//  Created by Rowling on 2021/10/13.
//

import UIKit

class XMLoadingView: UIView {
    
    /// 默认的加载失败文案
    public var defaultErrorTitle = "加载失败"
    
    var indecator : UIActivityIndicatorView!
    
    private var errorView = XMLoadingViewErrorView()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setUI()
    }
    
    private func setUI() {
        backgroundColor = .white
        indecator = UIActivityIndicatorView.init(style: UIActivityIndicatorView.Style.gray)
        addSubview(indecator)
    }
    
    public func showInView(fatherView : UIView) {
        if errorView.superview != nil {
            errorView.removeFromSuperview()
        }
        fatherView.addSubview(self)
        self.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        indecator.startAnimating()
    }
    
    public func hideView(anmation : Bool) {
        if self.superview == nil {
            return
        }
        if anmation {
            UIView.animate(withDuration: 0.5) {
                self.alpha = 0
            } completion: { ok in
                if ok {
                    self.removeFromSuperview()
                    self.indecator.stopAnimating()
                    self.alpha = 1
                }
            }
        } else {
            removeFromSuperview()
            indecator.stopAnimating()
        }
    }
    
    public func showError(_ title : String? = nil) {
        errorView.label.text = title ?? defaultErrorTitle
        addSubview(errorView)
        errorView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        indecator.center = CGPoint(x: zm.width / 2, y: zm.height / 2)
    }
    
    deinit {
        print("XMLoadingView is deinit")
    }
    
}

class XMLoadingViewErrorView : UIView {
    var label = ZMUtil.label(title: "加载失败", color: UIColor.zm.color666666(), font: UIFont.systemFont(ofSize: 14))
    var backBtn = ZMUtil.button(title: "返回", font: UIFont.systemFont(ofSize: 16), color: UIColor.zm.color666666())
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
        setUI()
    }
    
    func setUI() {
        backgroundColor = .white
        backBtn.snp.makeConstraints { make in
            make.width.equalTo(140)
            make.height.equalTo(40)
        }
        backBtn.layer.cornerRadius = 8
        backBtn.layer.borderWidth = 0.5
        backBtn.layer.borderColor = UIColor.zm.color666666().cgColor
        let stackView = UIStackView()
        stackView.axis = .vertical
        stackView.addArrangedSubview(label)
        label.numberOfLines = 6
        stackView.addArrangedSubview(backBtn)
        stackView.alignment = .center
        stackView.spacing = 8
        
        addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.right.lessThanOrEqualToSuperview().offset(-12)
        }
        backBtn.addTarget(self, action: #selector(autoGoBack), for: .touchUpInside)
    }
    
    @objc func autoGoBack() {
        if let vc = zm.getFaterVC() {
            vc.navigationController?.popViewController(animated: true)
        }
    }
}
